using System;
using System.Collections.Generic;
using System.Text;
using PH.Common;

namespace Hoshimi
{
    public class IPCreatorBot_CloseIPState : State
	{
		#region Singleton

		// Fields
		protected static IPCreatorBot_CloseIPState instance;

		// Constructor
		protected IPCreatorBot_CloseIPState() : base() { }

		// Methods
		public static IPCreatorBot_CloseIPState Instance()
		{
			// Uses "Lazy initialization"
			if( instance == null ) 
				instance = new IPCreatorBot_CloseIPState();

			return instance;
		}

		#endregion

		public override bool Play( Unit unit )
		{
			IPCreatorBot bot = (IPCreatorBot) unit;

            bot.SetName("CloseIP");

            // We can't give orders to the bot. We stop
			if ( bot.State != NanoBotState.WaitingOrders && bot.State != NanoBotState.Moving ) 
			{
				return true;
			}

			if (!bot.IsIPOpen())
			{
				unit.myStateMachine.ChangeState( IPCreatorBot_MoveState.Instance() );
				return true;
			}

			bot.StopIfMoving();

			if (bot.CloseIP())
			{
				unit.myStateMachine.ChangeState( IPCreatorBot_MoveState.Instance() );
				return true;
			}
			
            return true;
		}
	}
}